package com.controller;

import com.bean.*;
import com.bean.dangBean.Balance;
import com.service.*;
import com.util.OrderAssistant;
import com.util.PageAssistant;
import com.vo.CommodityVo;
import com.vo.PlaceOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("lookCommodity")
public class XiaoXinController {

//    注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
    @Autowired
    private ChooseCommodityService chooseCommodityService;

    @Autowired
    private PlaceOrderService placeOrderService;

    //超市业务逻辑service
    @Autowired
    private BalanceService balanceService;

    //库存
    @Autowired
    private StockService stockService;


    //退货记录表
    @Autowired
    private RetreateService retreateService;

    @Autowired
    private MarketServices marketServices;

    //计算价格的病态变量
     public static Double total = 0.00;

     public static Double tota2 = 0.00;

    public static Double tota3 = 0.00;



     //退货模块的查询订单
    @RequestMapping("lookReturn")
     public String lookReturn(Model model, HttpSession session, HttpServletRequest request,PlaceOrder place){
        System.out.println("需要查询的订单号:"+place);

        //这里需要判断一下订单号的状态
//        List<PlaceOrder> placeOrderList1 = placeOrderService.find(place);
//        Integer commodityStaus = placeOrderList1.get(0).getCommodityStaus();

        if (placeOrderService.find(place) != null || placeOrderService.find(place).get(0).getCommodityStaus() == 0){
//            if (placeOrderService.find(place).get(0).getCommodityStaus() == 1) {
                model.addAttribute("orderId", place.getOrderId());
                //查询订单号
                List<PlaceOrder> placeOrders = placeOrderService.find(place);
                if (placeOrders.size() == 1) {
                    //获取商品id
                    String commodityId = placeOrders.get(0).getCommodityId();
                    System.out.println("commodityId:" + commodityId);
                    //进行分割
                    String[] commodityIds = commodityId.split(",");


                    //拿到String类型的商品数量
                    String commodityCount = placeOrders.get(0).getCommodityCount();
                    //进行分割
                    String[] commodityCounts = commodityCount.split(",");

                    for (int i = 0; i < commodityCounts.length; i++) {
                        System.out.println("commodityCounts:" + commodityCounts[i]);
                    }

                    Map<String, Object> map = new HashMap<>();
                    List<ChooseCommodity> placeOrderList = new ArrayList<>();
                    for (int i = 0; i < commodityIds.length; i++) {
                        map.put("orderId", commodityIds[i]);
                        placeOrderList.add(chooseCommodityService.query(map).get(i));
                        System.out.println(placeOrderList.get(i).getName());
                        int Amonut = Integer.parseInt(commodityCounts[i]);
                        placeOrderList.get(i).setAmonut(Amonut);
                        map.clear();
                    }

                    model.addAttribute("lookReturn", placeOrderList);

                    System.out.println("placeOrderList" + placeOrderList.size());
                } else {
                    model.addAttribute("lookReturn", null);
                }
            }else {
            System.out.println("没有此订单");
        }
//        }



        return "Return";
     }


     //退货退指定单号的单个或多个商品的退款功能
    @RequestMapping("singleRefund")
    public String singleRefund(Model model, HttpSession session, HttpServletRequest request, Retreat retreat){
        //1.首先获取商品id，进行分割，判断长度
        String commodityId = retreat.getCommodityId();
        String[] split = commodityId.split(",");
        System.out.println(split);
        System.out.println("退货数据:"+retreat);


        return "";
}


    //退货退整单的退款功能
    @RequestMapping("singleWhole")
    public String singleWhole(Model model, HttpSession session, HttpServletRequest request, Retreat retreat){
        System.out.println("进入退货界面");
        System.out.println(retreat);

        //1.判断，当订单号不为null，退货理由不为空，商品id为空，商品数量为空，总金额为null
       if (retreat.getOrderId() != null && !retreat.getRefundReason().equals("") && retreat.getCommodityId().equals("") && retreat.getCommodityCount().equals("") &&

       retreat.getTotalPrice() == null){


        Integer orderId = retreat.getOrderId();
        System.out.println("您的整个订单号:"+orderId+"申请了退款!");

        //2.首先获取到订单号
        //通过订单号查询
        List<PlaceOrder> placeOrderList = placeOrderService.find(new PlaceOrder(orderId));
       //3.获取到订单的金额
        Double totalPrice = placeOrderList.get(0).getTotalPrice();
        //4.先进行对超市余额的修改
           Map<String,Object> map = new HashMap<>();
           List<Balance> query = balanceService.query(map);
           Integer id = query.get(0).getId();
           System.out.println("该账户id:"+id);
           //5.查询超市余额
           Double balance = query.get(0).getBalance();
           System.out.println("余额还有:"+balance);
           balance+=totalPrice;
           //修改
           int modify = balanceService.modify(new Balance(id, balance));
           if (modify > 0){
               System.out.println("退款余额已到账!");
               //6.获取该订单id
               Integer orderId1 = placeOrderList.get(0).getOrderId();
               System.out.println("订单id："+orderId1);
               //7.获取该订单的商品的id
               String commodityId = placeOrderList.get(0).getCommodityId();
               System.out.println("商品id："+commodityId);
               //8.获取该订单的商品的数量
               String commodityCount = placeOrderList.get(0).getCommodityCount();
               System.out.println("数量："+commodityCount);
               //9.获取退款理由
               String refundReason = retreat.getRefundReason();
               System.out.println("退款理由："+refundReason);
               //10.获取当前时间
               SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                Date date = new Date();
               String format = df.format(date);
               System.out.println("当前时间："+format);
               System.out.println("*********************************************");
               Retreat retreat1 = new Retreat(orderId1, commodityId, commodityCount, totalPrice, refundReason, date);
               System.out.println("retreat1:"+retreat1);
               //11.对退货数据表添加数据
               int save = retreateService.save(retreat1);
               if (save > 0){
                   System.out.println("添加退货数据表成功！");
                   //12.删除此订单
                   int remove = placeOrderService.remove(new PlaceOrder(orderId1));
                   if (remove > 0){
                       System.out.println("删除订单完成！");

                       return "Return";
                   }
               }
           }
       }else if (retreat.getOrderId() != null && !retreat.getRefundReason().equals("") && !retreat.getCommodityId().equals("") && !retreat.getCommodityCount().equals("") &&

               retreat.getTotalPrice() == null){
           System.out.println("进入退单个商品界面！");
           //1.进入退单个商品界面
           //2.获取订单号
           Integer orderId = retreat.getOrderId();
           //3.获取商品id
           Integer commodityId = Integer.parseInt(retreat.getCommodityId());
           System.out.println("传过来的商品id："+commodityId);
           //4.获取商品数量
           int commodityCount1 = Integer.parseInt(retreat.getCommodityCount());
           //5.获取退款理由
           String refundReason = retreat.getRefundReason();
           //6.获取时间
           SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
           Date date = new Date();
           String format = df.format(date);
           //7.通过订单号id查到对应的 信息
           List<PlaceOrder> placeOrderList = placeOrderService.find(new PlaceOrder(orderId));
           //8.通过查到的集合，查询商品id和数量
           String commodityId1 = placeOrderList.get(0).getCommodityId();
           System.out.println("查到了商品id"+commodityId1);
           //9.对获取的商品id进行拆分
           String[] split = commodityId1.split(",");
           System.out.println("");
           String commodityCount = placeOrderList.get(0).getCommodityCount();
           System.out.println("查到了数量"+commodityCount);
           //10.对获取的商品数量进行拆分
//           String[] counts = commodityCount.split(",");

            //11.for循环去查询，通过相同的index去查询
//           for (int i = 0; i < split.length;i++){
//               System.out.println("进来了！");
               //12.if判断该下标的商品的id是否相等
//              if ( split[i].equals(commodityId)){
//                  //如果相同就拿到商品数量  //获取相同下标的商品的id
//                  Integer countss = Integer.parseInt(counts[i]);
//                  Integer commodityss = Integer.parseInt(split[i]);
//                  System.out.println("第二层！");

                  //13..通过查到的商品id去查询相对应的价格
//                  System.out.println("countss:"+countss);
//                  System.out.println("countss:"+commodityss);
                  //14通过商品id取查询它的单价
                  List<ChooseCommodity> commodityList = chooseCommodityService.find(new ChooseCommodity(commodityId));
                  Double price = commodityList.get(0).getPrice();
                  //15.计算出价格
                  Double prices = commodityCount1 *  price;
                  //16.对退货数据表添加数据
                  retreateService.save(new Retreat(orderId,retreat.getCommodityId(),retreat.getCommodityCount(),prices,refundReason,date));

//                  //17.对订货表的数据进行修改
//                  List<PlaceOrder> placeOrderList1 = placeOrderService.find(new PlaceOrder(orderId));
//                  //18.获取商品的id字符串
//                  String commodityId2 = placeOrderList1.get(0).getCommodityId();
//                  //19.获取商品的数量字符串
//                  String commodityCount1 = placeOrderList1.get(0).getCommodityCount();
//                  //20.创建两个字符串用来把更改过的数组依次存进去
//                  List<String> com = new ArrayList<>();//商品id数组
//                  List<String> cou = new ArrayList<>();//商品数量数组
//                  //21.对两个字符串进行分割
//                  String[] split1 = commodityId2.split(",");
//                  String[] split2 = commodityCount1.split(",");
//                  //22.进行循环添加
//                  for (int a =0; a < split1.length;a++){
//                      if(split1[a] != retreat.getCommodityId() && split2[a] != retreat.getCommodityCount()){
//                          com.add(split1[a]+",");
//                          cou.add(split2[a]+",");
//                      }
//                  }
//                  //23.商品id集合转字符串
//                  String s = com.toString();
//                  //24.商品数量集合转字符串
//                  String s1 = cou.toString();
//                  System.out.println("商品ID:"+s);
//                  System.out.println("商品数量:"+s1);
                  //24.对超市余额进行修改
                  Map<String,Object> maps = new HashMap<>();
                  //25.查询到账户余额
                  List<Balance> query = balanceService.query(maps);
                  //26.获取账户的id和账户余额
                  Integer id = query.get(0).getId();
                  Double balance = query.get(0).getBalance();
                  //27.相加
                  balance+=price;
                  //28.进行更改!
                  int modify = balanceService.modify(new Balance(id, balance));
                  if (modify > 0){
                      System.out.println("余额修改成功！");
                  }
//              }
//           }

       }else{
           //当以上两个条件都不满足的时候，就走到else，订单发生错误!
           System.out.println("订单发生错误！");
       }
        return "Return";
    }


     //查看订单
    @RequestMapping("chakan")
    public String chakanPlace(Model model, HttpSession session, HttpServletRequest request,PlaceOrderVo place,PageAssistant<ChooseCommodity> paCommodity, String page){
        System.out.println("进入查看place页面！");
        System.out.println(place);
        System.out.println("页面:"+page);
        //根据传过来的订单号进行查询
        List<PlaceOrder> placeOrders = placeOrderService.find(place.parse());

            //查询到对象后，拿到一个String类型的字符串商品id
            String commodityId = placeOrders.get(0).getCommodityId();
            System.out.println("commodityId:"+commodityId);
            //对商品id进行分割
            String[] commodityIds = commodityId.split(",");
            //拿到String类型的商品数量
            String commodityCount = placeOrders.get(0).getCommodityCount();
            //进行分割
            String[] commodityCounts = commodityCount.split(",");

            for (int i = 0; i < commodityCounts.length;i++){
                System.out.println("commodityCounts:"+commodityCounts[i]);
            }


            //
            Map<String,Object> map = new HashMap<>();
            //创建一个商品的list集合
            List<ChooseCommodity> placeOrderList = new ArrayList<>() ;

            //进行循环添加
            for (int i = 0; i < commodityIds.length;i++){
                map.put("orderId",commodityIds[i]);
                map.put("amonut",commodityCounts[i]);
                //查询到之后添加到List集合
                placeOrderList.add(chooseCommodityService.query(map).get(i));
//            System.out.println("数量:"+chooseCommodityService.query(map).get(i).getAmonut());
//            System.out.println(placeOrderList.get(i).getName());
                int Amonut = Integer.parseInt(commodityCounts[i]);
                placeOrderList.get(i).setAmonut(Amonut);
                map.clear();
            }



            model.addAttribute("placeOrderList",placeOrderList);

            System.out.println("placeOrderList"+placeOrderList.size());



        return "chakan";


    }



     @RequestMapping("remove")
     public String removePlace(Model model, HttpSession session, HttpServletRequest request,PlaceOrderVo placePay){
         System.out.println("进入删除place页面！");
         System.out.println(placePay);

         int remove = placeOrderService.remove(placePay.parse());
         if (remove == 1){
             System.out.println("删除成功！");
         }

         return "redirect:/lookCommodity/look";

     }
     //订货订单多单支付
     @RequestMapping("placePays")
     public String placePays(Model model, HttpSession session, HttpServletRequest request,PlaceOrderVo placeOrderVo){
         System.out.println("进入多单支付界面");
         System.out.println(placeOrderVo);
         tota2 = 0.00;

         //1.首先要把接收到的字符串订单号截取
         String orderIds = placeOrderVo.getOrderId();
         String[] orderIdY = orderIds.split(",");
         System.out.println("数组长度为:"+orderIdY.length);

         for (int i = 0; i < orderIdY.length;i++){
             int orderId = Integer.parseInt(orderIdY[i]);
             System.out.println("orderId:"+orderId);
             List<PlaceOrder> placeOrderList = placeOrderService.find(new PlaceOrder(orderId));
             Double totalPrice = placeOrderList.get(0).getTotalPrice();
             tota2 = totalPrice + tota2;
             System.out.println("总金额是:"+tota2);
         }

         Map<String,Object> map = new HashMap<>();
         List<Balance> query = balanceService.query(map);
         Integer id = query.get(0).getId();
         System.out.println("该账户id:"+id);
         //查询超市余额
         Double balance = query.get(0).getBalance();
         System.out.println("余额还有:"+balance);

        //计算出所有订单的金额后，进行扣款！
         String updateMsg = "";
         //判断全部订单的金额是否大于超市余额
         if(tota2 < balance){
             balance-=tota2;
             //进行查询账户和余额还有多少
             int modify = balanceService.modify(new Balance(id,balance));
             //查询结果
             if (modify > 0){
                 System.out.println("付款成功！");
                 updateMsg = "付款成功!";
                 model.addAttribute("msg",updateMsg);
                 //余额足够厚进行分批修改相应的订单状态
                for (int i = 0; i < orderIdY.length;i++){
                    int orderId = Integer.parseInt(orderIdY[i]);
                    placeOrderService.modify(new PlaceOrder(orderId,0));
                }

             }
         }else{
             System.out.println("余额不足！");
             updateMsg = "余额不足，付款失败!";
             model.addAttribute("msg",updateMsg);
         }



         return "hello";
     }


    //订货订单单支付
     @RequestMapping("placePay")
     public String placePay(Model model, HttpSession session, HttpServletRequest request,PlaceOrder place){
         System.out.println("进入支付界面！");
         System.out.println(place);

         //订货单支付界面，首先要先查询超市余额是否足够，然后进行扣款
         //然后对商品的库存进行增加，查询到该订单的数据，然后进行截取，
         // 然后查询到相对应的商品id，进行数量的添加

         //1.首先需要查到当前的订单的金额,先进行扣款
//         placeOrderService.find(new PlaceOrder(placePay.parse()));
//        int orderIds = (int)place.getOrderId();
//         System.out.println("订单号:"+orderIds);

         List<PlaceOrder> placeOrderList = placeOrderService.find(place);
         System.out.println("placeOrderList:"+placeOrderList);
         //订单金额
         Double totalPrice = placeOrderList.get(0).getTotalPrice();
         System.out.println(totalPrice);
         //2.拿到金额后先去判断这个金额是否小于超市的余额
         Map<String,Object> map = new HashMap<>();
         //查询账户
         List<Balance> query = balanceService.query(map);
         Integer id = query.get(0).getId();
         System.out.println("该账户id:"+id);
         //查询超市余额
         Double balance = query.get(0).getBalance();
         System.out.println("余额还有:"+balance);

         //3.进行扣款，扣款前首先判断订单金额是否小于超余额，大于的话则不能扣款
        String updateMsg = "";
        //判断订单金额是否大于超市余额
         if(totalPrice < balance){
             //进行扣款
             balance-=totalPrice;
             //修改超市账户的余额信息
             int modify = balanceService.modify(new Balance(id,balance));
             if (modify > 0){
                 System.out.println("付款成功！");
                 updateMsg = "付款成功!";
                 model.addAttribute("msg",updateMsg);
                 //获取订单号，然后进行强转成int类型
                 Integer orderId = (int)place.getOrderId();
                 //修改订单的状态
                placeOrderService.modify(new PlaceOrder(orderId,0));
             }
         }else{
             System.out.println("余额不足！");
             updateMsg = "余额不足，付款失败!";
             model.addAttribute("msg",updateMsg);
         }


         return "redirect:/lookCommodity/look";
     }

    //购物车
    @RequestMapping("look")
    public String LookCommodity(Model model, HttpSession session, HttpServletRequest request,
                                PageAssistant<PlaceOrder> placeOrderPageAssistant, OrderAssistant orderInfo,PlaceOrderVo placeOrder){

        System.out.println("进入购物车界面>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(placeOrderPageAssistant);
        System.out.println(orderInfo);



        Map<String, Object> map = new HashMap<>();

        map.put("order", orderInfo.sql());


        placeOrderPageAssistant.setCount(placeOrderService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", placeOrderPageAssistant.getStart());
        map.put("size", placeOrderPageAssistant.getSize());


        List<PlaceOrder> query = placeOrderService.query(map);

//        for (int i = 0; i < query.size(); i++){
//            query.get(i).get
//        }
        for (int i = 0; i < query.size(); i++) {
            if(query.get(i).getCommodityStaus()==0){
                query.remove(i);

            }
        }

        /*保存set*/
        placeOrderPageAssistant.setItems(query);


        //5.保存
        model.addAttribute("placeOrderPageAssistant", placeOrderPageAssistant);
        model.addAttribute("orderInfo", orderInfo);
        model.addAttribute("placeOrder",placeOrder);


        /*thymeleaf不需要加后缀*/
        return "lookcommodity";
    }



    //查看订单
    @RequestMapping("lookOrder")
    public String LookOrder(Model model, HttpSession session, HttpServletRequest request,
                                PageAssistant<PlaceOrder> placeOrderPageAssistant, OrderAssistant orderInfo,PlaceOrderVo placeOrder) {

        System.out.println("进入购物车界面>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(placeOrderPageAssistant);
        System.out.println(orderInfo);


        Map<String, Object> map = new HashMap<>();

        map.put("order", orderInfo.sql());


        placeOrderPageAssistant.setCount(placeOrderService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", placeOrderPageAssistant.getStart());
        map.put("size", placeOrderPageAssistant.getSize());

        List<PlaceOrder> query = placeOrderService.query(map);
        for (int i = 0; i < query.size(); i++) {
            if(query.get(i).getCommodityStaus()==1){
                query.remove(i);

            }
        }
        /*保存set*/
        placeOrderPageAssistant.setItems(query);
        //5.保存
        model.addAttribute("placeOrderPage", placeOrderPageAssistant);
        model.addAttribute("orderInfo", orderInfo);
        model.addAttribute("placeOrder", placeOrder);


        /*thymeleaf不需要加后缀*/
        return "lookOrder";

    }
    //直接添加到订货数据表
    @RequestMapping("addshopping")
    public String add(Model model, HttpSession session, HttpServletRequest request,
                      PageAssistant<ChooseCommodity> paCommodity, OrderAssistant order, CommodityVo commodity){

        System.out.println("进入添加到购物车界面>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(order);
        System.out.println(commodity);
//        System.out.println(commodity.getAmonut());

        /*
         * 1.如何完成添加一个订单数据，
         * 首先需要一个订单号，这个订单不能随机，需要用到方法去数据库查询
         * 查询到订单号之后，查询到订单号的一个集合，然后取最后一个订单号
         * 作为基准，然后自增1，然后获取订单号
         * 2.获取商品id
         * 3.获取商品数量
         * 4.获取当前时间
         * 5.状态默认为1，1为未支付
         * 6.计算总金额
         * */
        //每次进来total都要初始化，要不然会重叠
        total = 0.00;

        Map<String,Object> map = new HashMap<>();

        //查询无条件的订单表数据
        List<PlaceOrder> queryList = placeOrderService.query(map);
        //获取有几条数据，得到一个数量
        int size = queryList.size();

        //然后根据这个数量，去拿到最后面的一个订单号
        Integer orderId = queryList.get(size-1).getOrderId();
        System.out.println(size);

        System.out.println(orderId);
        //然后生成一个新的订单号
        orderId++;


        //3.获取当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String orderDate = format.format(date);
        System.out.println(orderDate);




        //1.获取单价
        String priceA = commodity.getPrice();
        String[] prices = priceA.split(",");
        //2.获取商品id
        String idA = commodity.getId();
//        String[] ids = idA.split(",");
//        System.out.println("idA"+idA.toString());
        //1.获取数量
        String amonutA = commodity.getAmonut();
        String[] amonuts = amonutA.split(",");
//        for(int i=0;i<ids.length;i++){
//            System.out.println("ids:"+ids[i]);
//        }
        for(int i=0;i<amonuts.length;i++){
            System.out.println("amonuts:"+amonuts[i]);
        }

        for(int i=0;i<amonuts.length;i++){

            int commodityCount = Integer.parseInt(amonuts[i]);
//            Integer commodityId = Integer.parseInt(ids[i]);
            Double price = Double.parseDouble(prices[i]);
            Double totalPrice = commodityCount*price;
//            System.out.println("totalPrice:" +totalPrice);
//            System.out.println(new PlaceOrder(1, commodityId, commodityCount, (new Date()), 1, totalPrice));
//            placeOrderService.save(new PlaceOrder(orderId, commodityId, commodityCount, (new Date()), 1, totalPrice));
//            System.out.println("添加商品第"+(i+1)+"次！！！！");
            total = totalPrice + total;
            System.out.println(total);
        }
//        String id = idA.toString();
//        String amount = amonutA.toString();
        int save = placeOrderService.save(new PlaceOrder(orderId, idA, amonutA, (new Date()), 1, total));

        //保存
        String updateMsg = "添加成功!";
        String updateClass = "update-suc";
        if(save != 1){
            updateMsg = "添加失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);




        return "redirect:/lookCommodity/query";
    }


    @RequestMapping("query")
    public String query(Model model, HttpSession session, HttpServletRequest request,
                        PageAssistant<ChooseCommodity> paCommodity, OrderAssistant order,ChooseCommodity commodity){

        System.out.println("进入查看商品界面>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        System.out.println(paCommodity);
        System.out.println(order);


        /*1.创建参数集合，因为query那边是map类型，存储自己需要查询的类型包括分页和排序*/
        Map<String, Object> map = new HashMap<>();

        //设置排序参数
        /*2.设置排序，如果没有给参数的话，就按照默认的asc排序了*/
        map.put("order", order.sql());



        //3.查询总记录数，调用xml当中的selectCount方法查询总记录数
        paCommodity.setCount(chooseCommodityService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", paCommodity.getStart());
        map.put("size", paCommodity.getSize());


        List<ChooseCommodity> query = chooseCommodityService.query(map);


        /*保存set*/
        paCommodity.setItems(query);

        for (int i = 0; i <paCommodity.getItems().size(); i++) {
            System.out.println("对象："+ paCommodity.getItems().get(i));
        }
        //5.保存
        model.addAttribute("paCommodity", paCommodity);
        model.addAttribute("orderInfo", order);
        model.addAttribute("commodity",commodity);



        /*thymeleaf不需要加后缀*/
        return "Choose1";

    }

    //跳转return页面的方法
    @RequestMapping("recording")
    public String recording(Model model, HttpSession session, HttpServletRequest request,
                            PageAssistant<Retreat> paRetreat, OrderAssistant order,Retreat retreat){
        System.out.println("进入查看退货记录页");
        System.out.println("retreat:"+retreat);

        Map<String, Object> map = new HashMap<>();

        map.put("order", order.sql());



        //3.查询总记录数，调用xml当中的selectCount方法查询总记录数
        paRetreat.setCount(retreateService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", paRetreat.getStart());
        map.put("size", paRetreat.getSize());


        List<Retreat> query = retreateService.query(map);


        /*保存set*/
        paRetreat.setItems(query);

        for (int i = 0; i <paRetreat.getItems().size(); i++) {
            System.out.println("对象："+ paRetreat.getItems().get(i));
        }
        //5.保存
        model.addAttribute("paRetreat", paRetreat);
        model.addAttribute("orderInfo", order);
        model.addAttribute("commodity",retreat);





        ;
        return "retreat";
    }

    //商品库存查询
    @RequestMapping("Stock")
    public String commodityStock(Model model, HttpSession session, HttpServletRequest request,
                                 PageAssistant<Stock> paStock, OrderAssistant order, Stock stock){
        System.out.println("进入查看库存页面");
        System.out.println(stock);

        System.out.println(paStock);
        System.out.println(order);



        Map<String, Object> map = new HashMap<>();

        map.put("order", order.sql());


        paStock.setCount(stockService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", paStock.getStart());
        map.put("size", stockService.query(map).size());


        List<Stock> query = stockService.query(map);


        paStock.setItems(query);


        //5.保存
        model.addAttribute("placeStock", paStock);
        model.addAttribute("orderInfo", order);



        /*thymeleaf不需要加后缀*/
        return "Stock";
    }

    //商品入库
    @RequestMapping("Warehousing")
    public String Warehousing(Model model, HttpSession session, HttpServletRequest request,PlaceOrder place,Stock stock){
        System.out.println("进入商品入库界面");
        System.out.println(place);
        //1.首先查询此订单
        List<PlaceOrder> placeOrderList = placeOrderService.find(place);
        //2.获取String类型的商品id字符串
        String commodityId = placeOrderList.get(0).getCommodityId();
        System.out.println("订单中的商品ID:"+commodityId);
        //3.获取String类型的商品数量字符串
        String commodityCount = placeOrderList.get(0).getCommodityCount();
        System.out.println("订单中的商品数量:"+commodityCount);
        //4.对商品字符串进行分割
        String[] commodityIds = commodityId.split(",");
        //5.对商品数量字符串进行分割
        String[] commodityCounts = commodityCount.split(",");

        //6.创建一个Map
        Map<String,Object> map = new HashMap<>();
        //7.无条件查询库存对象
        List<Stock> query = stockService.query(map);

        for (int i = 0; i < commodityIds.length;i++){
            int parseInt = Integer.parseInt(commodityIds[i]);
            List<Stock> stocks = stockService.find(new Stock(parseInt));
           Integer commodityCount1 = stocks.get(0).getCommodityCount();
            int a = Integer.parseInt(commodityCounts[i]);

            int c = a+commodityCount1;
            int modify = stockService.modify(new Stock(parseInt, c));
            System.out.println("修改成功"+modify+"次");
            System.out.println(c);

        }




//        return "redirect:/lookCommodity/Stock;
        return "redirect:/lookCommodity/Stock";
    }



    //跳转return页面的方法
    @RequestMapping("return")
    public String Return(){
        System.out.println("进行跳转到return页面");
        return "Return";
    }


    //商品出库查询数据
    @RequestMapping("OutStock")
    public String OutStock(Model model, HttpSession session, HttpServletRequest request,
                           PageAssistant<Stock> paStock, OrderAssistant order, Stock stock){
        System.out.println("商品出库界面！");
        System.out.println(stock);

        System.out.println(paStock);
        System.out.println(order);



        Map<String, Object> map = new HashMap<>();

        map.put("order", order.sql());


        paStock.setCount(stockService.count(map));

        //4.设置分页参数，这是拿到其中的 默认的数值
        map.put("start", paStock.getStart());
        map.put("size", stockService.query(map).size());


        List<Stock> query = stockService.query(map);


        paStock.setItems(query);


        //5.保存
        model.addAttribute("placeStock", paStock);
        model.addAttribute("orderInfo", order);



        /*thymeleaf不需要加后缀*/
        return "OutStock";
    }

    //商品出库
    @RequestMapping("out")
    public String Out(Model model, HttpSession session, HttpServletRequest request, Stock stock,Market market) {
        System.out.println("商品出库界面！");
        System.out.println(stock);
        //1.首先查询此商品id
        Integer commodityId1 = stock.getCommodityId();
        //去商品表里面查询该商品
        List<Market> marketList = marketServices.find(new Market(commodityId1));
        //2.获去该商品id的数量
        Integer amount = marketList.get(0).getAmount();
        System.out.println("超市库存的数量:"+amount);
        //获取传过来的数量
        Integer commodityCount2 = stock.getCommodityCount();
        System.out.println("传过来的数量:"+commodityCount2);
        //进行相加赋值
        Integer amount2= amount +commodityCount2;
        System.out.println("超市库存:"+amount2);
        //进行对商品表的数量进行修改
        int modify = marketServices.modify(new Market(commodityId1, amount2));
        if (modify > 0){
            System.out.println("商品出库成功！");
            List<Stock> stocks = stockService.find(new Stock(commodityId1));
            Integer commodityCount = stocks.get(0).getCommodityCount();
            commodityCount-=stock.getCommodityCount();
        }

        return "redirect:/lookCommodity/OutStock";
    }


}
